﻿using System;
using System.Web.Mvc;
using BlueSky.Web.Extentions;

namespace BlueSky.Web
{
    /// <summary>
    /// Typed model binder base class.
    /// </summary>
    /// <typeparam name="TModel">Type of model.</typeparam>
    public abstract class ModelBinderBase<TModel> : ModelBinderBase
    {
        public abstract TModel BindTypedModel(ControllerContext controllerContext, ModelBindingContext bindingContext);

        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            return BindTypedModel(controllerContext, bindingContext);
        }
    }


    /// <summary>
    /// Model binder base class.
    /// </summary>
    public abstract class ModelBinderBase : IModelBinder
    {
        public abstract object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext);

        /// <summary>
        /// Receives value of parametre from HttpContext.Request.
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="name">Parametre name.</param>
        /// <returns>Value of parametre. Null-if the parametre is not found.</returns>
        public static string GetHttpRequestParam(ControllerContext context, string name)
        {
            return context.HttpContext.Request.Get(name);
        }

        /// <summary>
        /// Receives value of parametre from HttpContext.Request with value by default.
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="name">Parametre name.</param>
        /// <param name="nullValue">Value if the parametre null (is not found).</param>
        /// <returns>Value of parametre. nullValue - if the parametre is not found.</returns>
        public static string GetHttpRequestParam(ControllerContext context, string name, string nullValue)
        {
            return context.HttpContext.Request.Get(name, nullValue);
        }

        /// <summary>
        /// Receives the typified value of parametre from HttpContext.Request.
        /// </summary>
        /// <typeparam name="T">Parametre type.</typeparam>
        /// <param name="context">Context.</param>
        /// <param name="name">Parametre name.</param>
        /// <returns>Value of parametre. null - if the parametre is not found.</returns>
        public static T GetHttpRequestParam<T>(ControllerContext context, string name)
        {            
            return context.HttpContext.Request.Get<T>(name);
        }

        /// <summary>
        /// Receives the typified value of parametre from HttpContext.Request.
        /// </summary>
        /// <typeparam name="T">Parametre type.</typeparam>
        /// <param name="context">Context.</param>
        /// <param name="name">Parametre name.</param>
        /// <param name="formatInvalidValue">Value by default in a case if parametre does not correspond to type T.</param>
        /// <returns>Value of parametre. null - if the parametre is not found, formatInvalidValue - if the parametre does not correspond to type T.</returns>
        public static T GetHttpRequestParam<T>(ControllerContext context, string name, T formatInvalidValue)
        {
            return context.HttpContext.Request.Get<T>(name, formatInvalidValue);
        }

        /// <summary>
        /// Receives the typified value of parametre from HttpContext.Request.
        /// </summary>
        /// <typeparam name="T">Parametre type.</typeparam>
        /// <param name="context">Context.</param>
        /// <param name="name">Parametre name.</param>
        /// <param name="formatInvalidValue">Value by default in a case if parametre does not correspond to type T.</param>
        /// <param name="nullValue">Value by default if the parametre is not found.</param>
        /// <returns>Value of parametre. nullValue - if the parametre is not found, formatInvalidValue - if the parametre does not correspond to type T.</returns>
        public static T GetHttpRequestParam<T>(ControllerContext context, string name, T formatInvalidValue, T nullValue)
        {
            return context.HttpContext.Request.Get<T>(name, formatInvalidValue, nullValue);
        }
    }
}
